package com.example.zluserservice.service.remote;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.zluserservice.client.RoleFeignClient;
import com.example.zluserservice.dto.response.RoleResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @Author zhenliu（孙凌岳）
 * @Description 远程服务调用包装类：用来加强登录的可靠性
 * @Date 2025/6/23 23:30
 * @Version 1.0
 */
@Service
@Slf4j
public class RoleRemoteService {
    private final RoleFeignClient roleFeignClient;

    public RoleRemoteService(RoleFeignClient roleFeignClient) {
        this.roleFeignClient = roleFeignClient;
    }

    @SentinelResource(value = "roleFeignClient_findRoleResDTOListByUserId",
            blockHandler = "handleRoleBlock",
            fallback = "handleRoleFallback")
    public List<RoleResDTO> findRoleResDTOListByUserId(Long userId) {
        return roleFeignClient.findRoleResDTOListByUserId(userId);
    }

    public List<RoleResDTO> handleRoleBlock(Long userId, BlockException ex) {
        log.info(">>>>>>>>>>触发熔断方法");
        //限流或熔断时返回空列表，避免登录失败
        return Collections.emptyList();
    }

    public List<RoleResDTO> handleRoleFallback(Long userId, Throwable ex) {
        log.info(">>>>>>>>>>触发Fallback方法");
        //业务异常降级处理，日志记录等
        return Collections.emptyList();
    }
}